package com.ruibang.glass.produce.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruibang.glass.produce.constant.Constants;
import com.ruibang.glass.produce.domain.req.HotSingleBoardDefectReq;
import com.ruibang.glass.produce.domain.req.StirringRodTrendChart;
import com.ruibang.glass.produce.domain.resp.HotSingleBoardDefectSummary;
import com.ruibang.glass.produce.domain.resp.HotSingleBoardDefectTrendChart;
import com.ruibang.glass.produce.domain.resp.HotSingleBoardFlowTrendChart;
import com.ruibang.glass.produce.mapper.HotSingleBoardDefectMapper;
import com.ruibang.glass.produce.service.HotSingleBoardDefectService;
import com.ruibang.glass.produce.util.ExcelStyleUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: songJinKang
 * @CreateTime: 2024-05-15  10:40
 * @Description: TODO
 * @Version: 1.0
 */

@Slf4j
@Service
public class HotSingleBoardDefectServiceImpl implements HotSingleBoardDefectService {
    @Autowired
    private HotSingleBoardDefectMapper hotSingleBoardDefectMapper;

    @Override
    public HotSingleBoardDefectSummary defectSummary(HotSingleBoardDefectReq hotSingleBoardDefectReq) {
        //默认时间
        defaultTime(hotSingleBoardDefectReq);
        return hotSingleBoardDefectMapper.defectSummary(hotSingleBoardDefectReq);
    }

    @Override
    public HotSingleBoardDefectTrendChart defectQuantityTrendChart(HotSingleBoardDefectReq hotSingleBoardDefectReq) {
        //默认时间
        defaultTime(hotSingleBoardDefectReq);
        List<HotSingleBoardDefectSummary> hotSingleBoardDefectList = hotSingleBoardDefectMapper.defectQuantityTrendChart(hotSingleBoardDefectReq);
        HotSingleBoardDefectTrendChart hotSingleBoardDefectTrendChart = new HotSingleBoardDefectTrendChart();
        if (CollectionUtils.isNotEmpty(hotSingleBoardDefectList)) {
            //采集时间
            List<String> collectTime = hotSingleBoardDefectList.stream().map(e->e.getStartTime().format(Constants.DATE_TIME_FORMATTER)).collect(Collectors.toList());
            hotSingleBoardDefectTrendChart.setCollectTime(collectTime);

            //总数(气泡)
            List<Integer> bubble = hotSingleBoardDefectList.stream().map(HotSingleBoardDefectSummary::getBubbleTotalNum).collect(Collectors.toList());
            hotSingleBoardDefectTrendChart.setBubbleTotalNum(bubble);
            //0-150(气泡)
            List<Integer> b1 = hotSingleBoardDefectList.stream().map(HotSingleBoardDefectSummary::getBubbleNum1).collect(Collectors.toList());
            hotSingleBoardDefectTrendChart.setBubbleNum1(b1);
            //150-300(气泡)
            List<Integer> b2 = hotSingleBoardDefectList.stream().map(HotSingleBoardDefectSummary::getBubbleNum2).collect(Collectors.toList());
            hotSingleBoardDefectTrendChart.setBubbleNum2(b2);
            //300-500(气泡)
            List<Integer> b3 = hotSingleBoardDefectList.stream().map(HotSingleBoardDefectSummary::getBubbleNum3).collect(Collectors.toList());
            hotSingleBoardDefectTrendChart.setBubbleNum3(b3);
            //500-1000(气泡)
            List<Integer> b4 = hotSingleBoardDefectList.stream().map(HotSingleBoardDefectSummary::getBubbleNum4).collect(Collectors.toList());
            hotSingleBoardDefectTrendChart.setBubbleNum4(b4);
            //1000以上(气泡)
            List<Integer> b5 = hotSingleBoardDefectList.stream().map(HotSingleBoardDefectSummary::getBubbleNum5).collect(Collectors.toList());
            hotSingleBoardDefectTrendChart.setBubbleNum5(b5);


            //总数(铂金)
            List<Integer> pt = hotSingleBoardDefectList.stream().map(HotSingleBoardDefectSummary::getPtTotalNum).collect(Collectors.toList());
            hotSingleBoardDefectTrendChart.setPtTotalNum(pt);
            //0-150(铂金)
            List<Integer> p1 = hotSingleBoardDefectList.stream().map(HotSingleBoardDefectSummary::getPtNum1).collect(Collectors.toList());
            hotSingleBoardDefectTrendChart.setPtNum1(p1);
            //150-300(铂金)
            List<Integer> p2 = hotSingleBoardDefectList.stream().map(HotSingleBoardDefectSummary::getPtNum2).collect(Collectors.toList());
            hotSingleBoardDefectTrendChart.setPtNum2(p2);
            //300-500(铂金)
            List<Integer> p3 = hotSingleBoardDefectList.stream().map(HotSingleBoardDefectSummary::getPtNum3).collect(Collectors.toList());
            hotSingleBoardDefectTrendChart.setPtNum3(p3);
            //500-1000(铂金)
            List<Integer> p4 = hotSingleBoardDefectList.stream().map(HotSingleBoardDefectSummary::getPtNum4).collect(Collectors.toList());
            hotSingleBoardDefectTrendChart.setPtNum4(p4);
            //1000以上(铂金)
            List<Integer> p5 = hotSingleBoardDefectList.stream().map(HotSingleBoardDefectSummary::getPtNum5).collect(Collectors.toList());
            hotSingleBoardDefectTrendChart.setPtNum5(p5);


            //总数(铂金)
            List<Integer> stone = hotSingleBoardDefectList.stream().map(HotSingleBoardDefectSummary::getStoneTotalNum).collect(Collectors.toList());
            hotSingleBoardDefectTrendChart.setStoneTotalNum(stone);
            //0-150(铂金)
            List<Integer> s1 = hotSingleBoardDefectList.stream().map(HotSingleBoardDefectSummary::getStoneNum1).collect(Collectors.toList());
            hotSingleBoardDefectTrendChart.setStoneNum1(s1);
            //150-300(铂金)
            List<Integer> s2 = hotSingleBoardDefectList.stream().map(HotSingleBoardDefectSummary::getStoneNum2).collect(Collectors.toList());
            hotSingleBoardDefectTrendChart.setStoneNum2(s2);
            //300-500(铂金)
            List<Integer> s3 = hotSingleBoardDefectList.stream().map(HotSingleBoardDefectSummary::getStoneNum3).collect(Collectors.toList());
            hotSingleBoardDefectTrendChart.setStoneNum3(s3);
            //500-1000(铂金)
            List<Integer> s4 = hotSingleBoardDefectList.stream().map(HotSingleBoardDefectSummary::getStoneNum4).collect(Collectors.toList());
            hotSingleBoardDefectTrendChart.setStoneNum4(s4);
            //1000以上(铂金)
            List<Integer> s5 = hotSingleBoardDefectList.stream().map(HotSingleBoardDefectSummary::getStoneNum5).collect(Collectors.toList());
            hotSingleBoardDefectTrendChart.setStoneNum5(s5);
        }
        return hotSingleBoardDefectTrendChart;
    }

    @Override
    public IPage<HotSingleBoardDefectSummary> pageQuery(HotSingleBoardDefectReq hotSingleBoardDefectReq) {
        //默认时间
        defaultTime(hotSingleBoardDefectReq);
        return hotSingleBoardDefectMapper.pageQuery(new Page<>(hotSingleBoardDefectReq.getPageNo(), hotSingleBoardDefectReq.getPageSize()), hotSingleBoardDefectReq);
    }

    @Override
    public List<HotSingleBoardFlowTrendChart> flowTrendChart(HotSingleBoardDefectReq hotSingleBoardDefectReq) {
        //默认时间
        defaultTime(hotSingleBoardDefectReq);
        return hotSingleBoardDefectMapper.flowTrendChart(hotSingleBoardDefectReq);
    }

    @Override
    public List<StirringRodTrendChart> stirringRodTrendChart(HotSingleBoardDefectReq hotSingleBoardDefectReq) {
        //默认时间
        defaultTime(hotSingleBoardDefectReq);
        return hotSingleBoardDefectMapper.stirringRodTrendChart(hotSingleBoardDefectReq);
    }

    @Override
    public void export(HotSingleBoardDefectReq hotSingleBoardDefectReq, HttpServletResponse response) {
        hotSingleBoardDefectReq.setPageNo(-1);
        hotSingleBoardDefectReq.setPageSize(-1);
        IPage<HotSingleBoardDefectSummary> page = this.pageQuery(hotSingleBoardDefectReq);
        if (page != null && CollectionUtils.isNotEmpty(page.getRecords())) {
            try {
                EasyExcel.write(response.getOutputStream(), HotSingleBoardDefectSummary.class)
                        .registerWriteHandler(ExcelStyleUtils.createTableStyle())
                        .sheet("history_data").doWrite(page.getRecords());
            } catch (IOException e) {
                log.error("数据导出失败：{}", e.getMessage());
            }
        }
    }


    /**
     * @Description: 设置默认查询时间
     * @version v1.0
     * @author songJinKang
     * @date 2024-05-15 11:08
     */
    private void defaultTime(HotSingleBoardDefectReq hotSingleBoardDefectReq) {
        //默认时间
        if (hotSingleBoardDefectReq.getStartTime() == null || hotSingleBoardDefectReq.getEndTime() == null) {
            LocalDateTime endTime = LocalDateTime.now();
            LocalDateTime startTime = endTime.minusDays(1);
            hotSingleBoardDefectReq.setStartTime(startTime);
            hotSingleBoardDefectReq.setEndTime(endTime);
        }
    }
}
